<?php
	require_once('includes/bootstrap.php');
	
	if (empty($_SESSION[SESSION_NAME]))
	{
		header("HTTP/1.0 404 Not Found");
		exit;
	}
	
	$email = '';
	$password = '';
	
	if (!empty($_POST))
	{
		$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
		$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
	
		// Check again that all the fields have been filled
		if (empty($email) || empty($password))
		{
			
		}
		else
		{
			$req = mysql_query("SELECT * 
													FROM clients 
													WHERE email LIKE '" . mysql_real_escape_string($email) . "'
														AND password = '" . mysql_real_escape_string($password) . "'");
			
			if ($row = mysql_fetch_assoc($req))
			{
				$_SESSION['client']= $row;
				
				header('location: ' . APPLICATION_URL);
				exit;
			}
			else
				$_SESSION['invalid_credentials'] = true;
		}
		
	}
?>

<html>
	<head>
		<?php include_once('includes/head.php'); ?>
	</head>

	<body>

		<?php include_once('includes/header.php'); ?>
		
		<h2>Connexion</h2>
		
		<script type="text/javascript">
		
			<?php if (!empty($_SESSION['invalid_credentials'])) { ?>
				noty({text: "L'email et/ou le mot de passe saisi sont invalides", layout: 'topCenter', type: 'success'});
			<?php unset($_SESSION['invalid_credentials']); ?>
			<?php } ?>
		
		</script>
		
		<form method="POST">
			
			<table>
				<tr>
					<td>Email</td>
					<td><input type="text" name="email" /></td>
				</tr>
				<tr>
					<td>Mot de passe</td>
					<td><input type="password" name="password" /></td>
				</tr>
				<tr>
					<td colspan="2" style="text-align: right"><button type="submit" id="Submit">Envoyer</button></td>
				</tr>
			</table>
			
		</form>
		
		<script type="text/javascript">
			$("input:text, input:password, textarea").addClass("ui-corner-all");
			
			$('form table').addClass('ui-widget ui-widget-content ui-corner-all');

			$("button").button();
		</script>

	</body>

</html>
